package com.sensePlusVR.setting.bluetooth;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.main.R;
import com.sensePlusVR.setting.bluetooth.BluetoothPlayer.BTEvent;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.widget.CircleDLProgressView;

@ContentView(R.layout.activity_setting_blue_unwrap)
public class BlueUnwrapActivity extends BaseActivity {
	private static final String TAG = BlueUnwrapActivity.class.getSimpleName();
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;

	@ViewInject(R.id.setting_blue_batteryLevel)
	private TextView setting_blue_batteryLevel;

	@ViewInject(R.id.setting_blue_equipment_iv)
	private ImageView setting_blue_equipment_iv;

	@ViewInject(R.id.setting_blue_progress)
	private CircleDLProgressView setting_blue_progress;

	private VRApplication vrApplication;
	private int batteryLevel;
	
	private String macName;

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}

	@Override
	public void beforeView() {
		super.beforeView();
		AppManager.getInstance().addActivity(this);
		vrApplication = (VRApplication) getApplication();
	}

	@Override
	public void initView() {
		super.initView();
		 macName = MySharedPreferences.getString(this,
				Constant.SP_BLUE_NAME);
		
		MyLog.d(TAG, "macName : " + macName);
		title_bar_title_tv.setText(macName);
		
		setting_blue_progress.setMaxCount(100);
		setting_blue_progress.setCircleStrokeWithSp(2.0f);
		setting_blue_progress.setBackCircleColor(getResources().getColor(
				R.color.blue_battery_pro_bg));
		setting_blue_progress.setScoreVisible(false);
		setting_blue_progress.setAnticlockwise();
		
		
		updateBaterry(vrApplication.isBTConnected());
		
	}

	@Event(value = { R.id.title_bar_left_ib, R.id.setting_blue_unwrap_btn })
	private void setViewOnClick(View view) {
		switch (view.getId()) {
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;
		case R.id.setting_blue_unwrap_btn:
			MyLog.d(TAG, "setting_blue_unwrap_btn");
			vrApplication.toBTDisconnect();
			MySharedPreferences.save(this, Constant.SP_BLUE_ADDRESS, "");
			MySharedPreferences.save(this, Constant.SP_BLUE_NAME, "");
			AppManager.getInstance().finishActivity(this);
			break;
		}
	}
	
	private void updateBaterry(boolean isConnected){
		
		MyLog.d(TAG, "updateBaterry isConnected : " + isConnected);
		
		if(isConnected){
			
			batteryLevel = vrApplication.getBatteryLevel();	
			
		}else{
			
			batteryLevel = 0;
		}
		
		MyLog.d(TAG, " battery level : " + batteryLevel);
		
		setting_blue_progress.setCurrentCount(batteryLevel);
		
		if(batteryLevel == 0){

			if (macName.equals(Constant.SENSE_TUBE)) {
				setting_blue_equipment_iv
						.setImageResource(R.drawable.blue_masturbation_cup_disable);
			} else if (macName.equals(Constant.SENSE_BAND)) {
				setting_blue_equipment_iv
						.setImageResource(R.drawable.blue_blacelet_disable);
			}
			setting_blue_batteryLevel.setText(Constant.SYMBOL_INTHEBAR);
			
		}else{

			if (macName.equals(Constant.SENSE_TUBE)) {
				setting_blue_equipment_iv
						.setImageResource(R.drawable.blue_masturbation_cup);
			} else if (macName.equals(Constant.SENSE_BAND)) {
				setting_blue_equipment_iv
						.setImageResource(R.drawable.blue_blacelet);
			}
			setting_blue_batteryLevel.setText(String.valueOf(batteryLevel)
					+ Constant.SYMBOL_PERCENTAGE);
			
		}
	}
	
	private BTEvent mBTEvent = new BTEvent() {
		
		@Override
		public void onConnected(final boolean isConnected) {
			
			MyLog.d(TAG, "isConnected : " + isConnected);
		
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						
						updateBaterry(isConnected);
						
					}
				});
			
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		vrApplication.setBTEvent(mBTEvent);
	}

	@Override
	protected void onPause() {
		super.onPause();
		vrApplication.setBTEvent(null);
	}
	
	
	


}
